<template>
  <div>
    <!--
       安装axios
       npm i axios
      -->

      <button @click="getStudents">获取学生信息</button>
      <button @click="getCars">获取汽车信息</button>
  </div>
</template>

<script>
//引入axios
import axios from 'axios'

export default {
  name: "App",
  components: {

  },
  data() {
    return {

    }
  },
  methods: {
    //发送ajax请求
    getStudents() {
      //let url = 'http://localhost:5000/students' //直接请求这个后端地址会跨域
      let url = 'http://localhost:8080/api/students' //开启代理服务器后请求本机的端口
      //axios.get发送get请求，返回一个Promise对象
      axios.get(url)
        .then(response=>{
            //请求成功时执行第一个回调
            console.log('成功', response.data)
        }, error => {
            //请求失败时执行第二个回调
            console.warn('失败', error.message)
        })
    },
    getCars() {
      let url = 'http://localhost:8080/v2/cars'
      axios.get(url)
        .then(response=>{
            console.log('成功', response.data)
        }, error => {
            console.warn('失败', error.message)
        })
    }
  }
}
</script>

<style>

</style>
